
#include "include/check_state.h"


//EtherCAT
ec_master_t *master=NULL;
ec_master_state_t master_state={};

ec_domain_t *domainServoInput=NULL ;
ec_domain_state_t domainServoInput_state={} ;
ec_domain_t *domainServoOutput =NULL;
ec_domain_state_t domainServoOutput_state ={};

uint8_t *domainOutput_pd=NULL ;
uint8_t *domainInput_pd =NULL;

ec_slave_config_t *sc_cooldrive0 =NULL;
ec_slave_config_state_t sc_cooldrive0_state={};
ec_slave_config_t *sc_cooldrive1 =NULL;
ec_slave_config_state_t sc_cooldrive1_state={} ;
/****************************************************************************/

void check_master_state(void)
{
    ec_master_state_t ms;

    ecrt_master_state(master, &ms);

    if (ms.slaves_responding != master_state.slaves_responding) {
        printf("found %u slave(s).\n", ms.slaves_responding);
    }
    if (ms.al_states != master_state.al_states) {
        printf("AL states: 0x%02X.\n", ms.al_states);
    }
    if (ms.link_up != master_state.link_up) {
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
    }

    master_state = ms;
}

/*****************************************************************************/

void check_slave0_config_states(void)
{
    ec_slave_config_state_t s;

    ecrt_slave_config_state(sc_cooldrive0, &s);

    if (s.al_state != sc_cooldrive0_state.al_state) {
        printf("CoolDrive0: State 0x%02X.\n", s.al_state);
    }
    if (s.online != sc_cooldrive0_state.online) {
        printf("CoolDrive0: %s.\n", s.online ? "online" : "offline");
    }
    if (s.operational != sc_cooldrive0_state.operational) {
        printf("CoolDrive0: %soperational.\n", s.operational ? "" : "Not ");
    }

    sc_cooldrive0_state = s;
}
/*****************************************************************************/

void check_slave1_config_states(void)
{
    ec_slave_config_state_t s;

    ecrt_slave_config_state(sc_cooldrive1, &s);

    if (s.al_state != sc_cooldrive1_state.al_state) {
        printf("CoolDrive1: State 0x%02X.\n", s.al_state);
    }
    if (s.online != sc_cooldrive1_state.online) {
        printf("CoolDrive1: %s.\n", s.online ? "online" : "offline");
    }
    if (s.operational != sc_cooldrive1_state.operational) {
        printf("CoolDrive1: %soperational.\n", s.operational ? "" : "Not ");
    }

    sc_cooldrive1_state = s;
}
/*****************************************************************************/

void check_domainInput_state(void)
{
    ec_domain_state_t ds;

    ecrt_domain_state(domainServoInput, &ds);

    if (ds.working_counter != domainServoInput_state.working_counter) {
        printf("Domain1: WC %u.\n", ds.working_counter);
    }
    if (ds.wc_state != domainServoInput_state.wc_state) {
        printf("Domain1: State %u.\n", ds.wc_state);
    }

    domainServoInput_state = ds;
}
/*****************************************************************************/

void check_domainOutput_state(void)
{
    ec_domain_state_t ds;

    ecrt_domain_state(domainServoOutput, &ds);

    if (ds.working_counter != domainServoOutput_state.working_counter) {
        printf("Domain1: WC %u.\n", ds.working_counter);
    }
    if (ds.wc_state != domainServoOutput_state.wc_state) {
        printf("Domain1: State %u.\n", ds.wc_state);
    }

    domainServoOutput_state = ds;
}
/*****************************************************************************/
